package com.demo.handphone.handphone;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.DigitsKeyListener;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.demo.handphone.handphone.utils.SPUtils;
import com.demo.handphone.handphone.utils.ToastUtil;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_userName;
    private EditText et_userPwd;
    private Button btn_login;
    private ImageView img_ip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉标题栏 有效
        setContentView(R.layout.activity_login);

        et_userName = (EditText) findViewById(R.id.et_lg_user);
        et_userPwd = (EditText) findViewById(R.id.et_lg_pwd);
        btn_login = (Button) findViewById(R.id.btn_lg_login);
        img_ip = (ImageView) findViewById(R.id.img_ip);

        if (String.valueOf(SPUtils.get(LoginActivity.this,"ip","")).equals("")) {
            ToastUtil.showToast("请先设置ip");
        } else {
            //获取到存储的值
//            MyApplication.m_Url = MyApplication.http + sp.getString("ip", "");
            et_userName.setText(String.valueOf(SPUtils.get(LoginActivity.this,"userName","")));
            et_userPwd.setText(String.valueOf(SPUtils.get(LoginActivity.this,"password","")));
        }


        img_ip.setOnClickListener(this);
        btn_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_lg_login:
                if (getSharedPreferences("position", Context.MODE_PRIVATE).getString("ip", "").equals("")) {
                    ToastUtil.showToast("请先设置IP地址");
                } else {
                    login();
                }
                break;
            case R.id.img_ip:

                setIp();
                break;
        }
    }

    private void login() {
        final String userName = et_userName.getText().toString().trim();
        final String pwd = et_userPwd.getText().toString().trim();
        if (userName.equals("")) {
            ToastUtil.showToast("请输入账号");
            return;
        }
        if (pwd.equals("")) {
            ToastUtil.showToast("请输入密码");
            return;
        }


    }

    private void setIp() {
        final EditText et = new EditText(LoginActivity.this);
        et.setHint("192.168.8.8:8888/Base");
        String digital = "0123456789.:/qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
        et.setKeyListener(DigitsKeyListener.getInstance(digital));
//                若已经保存ip，则显示
        if (!"".equals(String.valueOf(SPUtils.get(LoginActivity.this,"ip",""))) ) {
            et.setText(String.valueOf(SPUtils.get(LoginActivity.this,"ip","")));
        }

        new AlertDialog.Builder(LoginActivity.this)
                .setMessage("请输入ip地址")
                .setView(et)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        String ipString = et.getText().toString().trim();
                        SPUtils.put(LoginActivity.this, "ip", ipString);

                        ToastUtil.showToast("设置IP: " + et.getText().toString() + " 成功");
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                    }
                }).show();
    }
}
